package com.huan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huan.VO.PageVo;
import com.huan.VO.RoleVo;
import com.huan.VO.UserInfoVo;
import com.huan.domain.AppHttpCodeEnum;
import com.huan.domain.ResponseResult;
import com.huan.domain.entity.Role;
import com.huan.domain.entity.User;
import com.huan.mapper.UserMapper;
import com.huan.security.exception.SystemException;
import com.huan.service.UserService;
import com.huan.utils.BeanCopyUtil;
import com.huan.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 用户表(User)表服务实现类
 *
 * @author makejava
 * @since 2023-06-09 16:26:02
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public ResponseResult userInfo() {
        Long userId = SecurityUtils.getUserId();
        User user = getById(userId);
        UserInfoVo userInfoVo = BeanCopyUtil.resultVO(user, UserInfoVo.class);
        return ResponseResult.okResult(userInfoVo);
    }

    @Override
    public ResponseResult updateUserInfo(User user) {
        updateById(user);
        return ResponseResult.okResult();
    }
     @Autowired
    private PasswordEncoder passwordEncoder;
    @Override
    public ResponseResult register(User user) {
//        非空判断
//        重复判断
//        将密码格式修改
//        存入数据库
//        Spring Validation框架
        if (!StringUtils.hasText(user.getNickName())){
            throw new SystemException(AppHttpCodeEnum.FAIL_NICKNAME);
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getNickName, user.getNickName());
        if (count(wrapper)>0){
            throw  new SystemException(AppHttpCodeEnum.FAIL_NICKNAME);
        }
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        save(user);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getAllUser(Integer pageNum, Integer pageSize, String userName, String phonenumber, String status) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(User::getUserName,userName);
        wrapper.eq(User::getPhonenumber,phonenumber);
        Page<User> page = new Page<>();
        page.setPages(pageNum);
        page.setSize(pageSize);
        List<User> list = page(page, wrapper).getRecords();
        List<UserInfoVo> detailVos = BeanCopyUtil.resultAllVo(list, UserInfoVo.class);
        PageVo pageVo = new PageVo(detailVos, page.getTotal());
        return ResponseResult.okResult(pageVo);
    }


}

